package com.hnkywl.sts.action.zjgl;

import com.google.gson.Gson;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.entity.zjgl.Yhsx;
import com.hnkywl.sts.entity.zjgl.Yhsxmx;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.service.zjgl.YhsxService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 说明：
 * 包:        com.hnkywl.sts.action.sfgl
 * 类描述:    岗位Action类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Namespace("/zjgl/yhsx")
public class YhsxAction extends SimpleActionSupport {
    private YhsxService yhsxService;
    private Integer zt;
    //银行授信实例
    private Yhsx yhsx;
    //银行授信明细实例
    private Yhsxmx yhsxmx;
    //银行授信Id
    private Long yhsxId;
    //银行授信明细Id
    public Long yhsxmxId;
    //公司机构名称
    private String gsMc;
    //公司机构Id
    private Long gsId;
    private Date jkrqStart;
    private Date jkrqEnd;
    //申请人Id
    private Long sqrId;
    //制单人Id
    private Long zdrId;
    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows = 10;
    //远程ajax验证的值
    private String validateVal;
    //需要远程ajax验证的属性
    private String validateField;
    //当前时间
    private Date startDate;
    //还款参数
    private Long yhmcId;
    private String yhmc;
    private Long gsmcId;
    private String gsmc;
    private Long yhId;
    private String yhMc;
    private Double jkje;
    private String jkrq;

    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;

    private String sysDate;

    private Date yhsxmxHkrq;
    private Double yhsxmxHkje;

    //银行授信的查询分页显示
    @Action("yhsxList")
    public void yhsxList() {
        this.renderJson(this.yhsxService.findAllYhsx(gsId, gsMc,jkrqStart,jkrqEnd,yhMc,zt,page, rows));
    }

    @Action("yhsx")
    public String yhsx(){
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
        sysDate = simple.format(new Date());
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }

    @Action("yhsx_content")
    public String yhsxContent() {
        zdrId = YgService.getCurrentYg().getId();
        if (yhsxId != null) {
            //进入update页面
            yhsx = yhsxService.findYhsxDetail(yhsxId);
        } else{
            yhsx = new Yhsx();
            this.startDate = new Date();
        }
        return SUCCESS;
    }

    @Action("wlysx_content")
    public String wlyxsContent() {
        zdrId = YgService.getCurrentYg().getId();
        if (yhsxId != null) {
            //进入update页面
            yhsx = yhsxService.findYhsxDetail(yhsxId);
        } else{
            yhsx = new Yhsx();
            yhsx.setJkrq(new Date());
        }
        return SUCCESS;
    }

    /**
     * 方法名:     addYhsx
     * 功能描述:      增加银行授信
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     * 创建人:     zsp
     * 创建日期:   13-3-29上午9:47
     * 修改人:     zsp
     * 修改日期:   13-3-29上午9:47
     */
    @Action("add_yhsx")
    public void addYhsx() {
        try {
            this.yhsxService.saveYhsx(yhsx);
            renderText("{\"statusCode\":\"200\", \"message\":\"添加银行授信成功！\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("add_wlysx")
    public void addWlysx() {
        try {
            this.yhsxService.saveWlysx(yhsx);
            renderText("{\"statusCode\":\"200\", \"message\":\"添加物流园授信成功！\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 方法名:     updateYhsx
     * 功能描述:      修改银行授信
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     * 创建人:     zsp
     * 创建日期:   13-3-29上午11:50
     * 修改人:     zsp
     * 修改日期:   13-3-29上午11:50
     */
    @Action("update_yhsx")
    public void updateYhsx() {
        try {
            this.yhsxService.updateYhsx(yhsx);
            renderText("{\"statusCode\":\"200\",\"message\":\"修改银行授信成功！\"}");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("update_wlysx")
    public void updateWlysx() {
        try {
            this.yhsxService.updateWlysx(yhsx);
            renderText("{\"statusCode\":\"200\",\"message\":\"修改物流园授信成功！\"}");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("yhsx_Lxjs")
    public void yhsxLxjs() {
        try {
            Double lxe = this.yhsxService.yhsxLxjs(yhsxId,yhsxmxHkje,yhsxmxHkrq);
            renderText(lxe.toString());
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText(e.getMessage());
        }
    }


    /**
     * 方法名:     deleteYhsx
     * 功能描述:      删除数据库银行授信
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     * 创建人:     zsp
     * 创建日期:   13-3-29上午11:47
     * 修改人:     zsp
     * 修改日期:   13-3-29上午11:47
     */
    @Action("delete_yhsx")
    public void deleteYhsx() {
        try {
            this.yhsxService.deleteYhsx(yhsxId);
            renderText("{\"statusCode\":\"200\", \"message\":\"删除银行授信成功！\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }



    //银行授信--还款页面显示
    @Action("yhsx_yhhk")
    public String yhsxYhhk() {
        yhsx = yhsxService.findYhsxDetail(yhsxId);
        yhsxmx = new Yhsxmx();
        gsmcId = yhsx.getNbgs().getId();
        gsmc = yhsx.getNbgs().getJc();
        yhmcId = yhsx.getKhh().getId();
        yhmc = yhsx.getKhh().getQc();
        jkje = Double.parseDouble(yhsx.getJkje().toString());
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
        jkrq =simple.format(yhsx.getJkrq());
        return SUCCESS;
    }


    //银行授信--还款
    @Action("yhsx_hk")
    public void yhsxHk() {
        try {
            this.yhsxService.yhsxHk(yhsxId,yhsxmx,sqrId);
            renderText("{\"statusCode\":\"200\",\"message\":\"还款成功！\"}");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    //银行授信--详细信息
    @Action("yhsx_detail")
    public String yhsxDetail(){
        yhsx = yhsxService.findYhsxDetail(yhsxId);
        return SUCCESS;
    }

    //银行授信--还款
    @Action("yhsx_hkmx")
    public void yhsxHkmx(){
        this.renderJson(this.yhsxService.findYhsxHkmx(yhsx));
    }

    //银行授信确认 状态更改为1
    @Action("yhsx_confirm")
    public void yhsxConfirm() {
        try {
            yhsxService.yhsxConfirm(yhsxId,zt);
            if(zt==1){
                renderText("{\"statusCode\":\"200\", \"message\":\"付款单已确认！\", \"operationType\":\"delete\"}");
            }
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }

    }

    @Action("qxqr_yhsx")
    public void qxqrYhsx() {
        try {
            Yhsx yhsx  = this.yhsxService.findYhsxDetail(yhsxId);
            if(yhsx.getYhsxmxList().get(0).getInsertFlag()==3){
                renderText("{\"statusCode\":\"300\", \"message\":\"该银行授信已经导入财务系统！\", \"operationType\":\"delete\"}");
            }else{
                this.yhsxService.qxqrYhsx(yhsxId);
                renderText("{\"statusCode\":\"200\", \"message\":\"取消银行授信确认成功！\", \"operationType\":\"delete\"}");
            }
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 远程ajax验证
     */
    @Action("ajax_validate")
    public void ajaxValidate() {
        if (!yhsxService.ajaxValidate(yhsxmxId,validateField,validateVal))
            renderJson("{\"valid\":true}");
        else
            renderJson("{\"valid\":false}");
    }

    //===============getter and setter=============

    public Integer getZt() {
        return zt;
    }

    public void setZt(Integer zt) {
        this.zt = zt;
    }

    public Date getJkrqStart() {
        return jkrqStart;
    }

    public void setJkrqStart(Date jkrqStart) {
        this.jkrqStart = jkrqStart;
    }

    public Date getJkrqEnd() {
        return jkrqEnd;
    }

    public void setJkrqEnd(Date jkrqEnd) {
        this.jkrqEnd = jkrqEnd;
    }

    public Yhsx getYhsx() {
        return yhsx;
    }

    public void setYhsx(Yhsx yhsx) {
        this.yhsx = yhsx;
    }

    public Yhsxmx getYhsxmx() {
        return yhsxmx;
    }

    public void setYhsxmx(Yhsxmx yhsxmx) {
        this.yhsxmx = yhsxmx;
    }

    public Long getYhsxId() {
        return yhsxId;
    }

    public Long getZdrId() {
        return zdrId;
    }

    public void setZdrId(Long zdrId) {
        this.zdrId = zdrId;
    }

    public void setYhsxId(Long yhsxId) {
        this.yhsxId = yhsxId;
    }

    public String getGsMc() {
        return gsMc;
    }

    public void setGsMc(String gsMc) {
        this.gsMc = gsMc;
    }

    public Long getGsId() {
        return gsId;
    }

    public void setGsId(Long gsId) {
        this.gsId = gsId;
    }

    public Double getJkje() {
        return jkje;
    }

    public void setJkje(Double jkje) {
        this.jkje = jkje;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public String getValidateVal() {
        return validateVal;
    }

    public void setValidateVal(String validateVal) {
        this.validateVal = validateVal;
    }

    public String getValidateField() {
        return validateField;
    }

    public void setValidateField(String validateField) {
        this.validateField = validateField;
    }

    public Date getStartDate() {
        return startDate;
    }

    public Long getYhmcId() {
        return yhmcId;
    }

    public void setYhmcId(Long yhmcId) {
        this.yhmcId = yhmcId;
    }

    public String getYhmc() {
        return yhmc;
    }

    public void setYhmc(String yhmc) {
        this.yhmc = yhmc;
    }

    public Long getGsmcId() {
        return gsmcId;
    }

    public void setGsmcId(Long gsmcId) {
        this.gsmcId = gsmcId;
    }

    public String getYhMc() {
        return yhMc;
    }

    public void setYhMc(String yhMc) {
        this.yhMc = yhMc;
    }

    public Long getYhId() {
        return yhId;
    }

    public void setYhId(Long yhId) {
        this.yhId = yhId;
    }

    public String getGsmc() {
        return gsmc;
    }

    public void setGsmc(String gsmc) {
        this.gsmc = gsmc;
    }

    public String getJkrq() {
        return jkrq;
    }

    public void setJkrq(String jkrq) {
        this.jkrq = jkrq;
    }

    public Long getYhsxmxId() {
        return yhsxmxId;
    }

    public void setYhsxmxId(Long yhsxmxId) {
        this.yhsxmxId = yhsxmxId;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;

    }

    public Long getSqrId() {
        return sqrId;
    }

    public void setSqrId(Long sqrId) {
        this.sqrId = sqrId;
    }

    public String getSysDate() {
        return sysDate;
    }

    public void setSysDate(String sysDate) {
        this.sysDate = sysDate;
    }

    public Date getYhsxmxHkrq() {
        return yhsxmxHkrq;
    }

    public void setYhsxmxHkrq(Date yhsxmxHkrq) {
        this.yhsxmxHkrq = yhsxmxHkrq;
    }

    public Double getYhsxmxHkje() {
        return yhsxmxHkje;
    }

    public void setYhsxmxHkje(Double yhsxmxHkje) {
        this.yhsxmxHkje = yhsxmxHkje;
    }

    @Autowired
    public void setYhsxService(YhsxService yhsxService) {
        this.yhsxService = yhsxService;
    }

    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }

}
